package cn.iocoder.yudao.module.trade.service.giftcard;

import cn.iocoder.yudao.module.trade.dal.dataobject.giftcard.GiftCardReceiveGiveDO;
import cn.iocoder.yudao.module.trade.dal.mysql.giftcard.GiftCardReceiveGiveMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GiftCardReceiveGiveServiceImpl implements GiftCardReceiveGiveService {

    @Resource
    private GiftCardReceiveGiveMapper giftCardReceiveGiveMapper;

    @Override
    public List<GiftCardReceiveGiveDO> queryGiftCardReceiveGiver(Long receiveMemberUserId) {
        return giftCardReceiveGiveMapper.queryGiftCardReceiveGiver(receiveMemberUserId);
    }

    @Override
    public List<GiftCardReceiveGiveDO> queryGiftCardReceiveGiverTime(Long cardId) {
        return giftCardReceiveGiveMapper.queryGiftCardReceiveGiverTime(cardId);
    }


    @Override
    public void saveOrUpdateGiftCardReceiveGiveDO(GiftCardReceiveGiveDO giftCardReceiveGive) {
        giftCardReceiveGiveMapper.insertOrUpdate(giftCardReceiveGive);
    }

    @Override
    public GiftCardReceiveGiveDO queryByCardId(Long cardId) {
        List<GiftCardReceiveGiveDO> list = giftCardReceiveGiveMapper.queryByCardId(cardId);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public Long countGiveReceiveByCardId(Long cardId) {
        return giftCardReceiveGiveMapper.selectCount(GiftCardReceiveGiveDO::getGiftCardId, cardId);
    }

    @Override
    public void deleteGiveReceiveByCardId(Long cardId) {
        QueryWrapper<GiftCardReceiveGiveDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("gift_card_id", cardId);
        giftCardReceiveGiveMapper.delete(queryWrapper);
    }

}
